package com.qixianxian.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtil {
    private static String url = null;
    private static String user = null;
    private static String password = null;

    private DBUtil() {
    }

    static {
        Properties pro = new Properties();
        try {
            InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("database.properties");
            pro.load(in);
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Connection conn = null;

    public static Connection getConn() {
        if (conn == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url, user, password);
//                conn = DriverManager.getConnection(
//                        "jdbc:mysql://127.0.0.1:3306/informationmanger?characterEncoding=UTF-8",
//                        "root",
//                        "1998"
//                );
                conn.setAutoCommit(false);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return conn;
        }

        private static Statement stat = null;
        public static Statement getStat () {
            try {
                conn = getConn();
                stat = conn.createStatement();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return stat;
        }

        private static PreparedStatement pstat = null;
        public static PreparedStatement getPstat (String sql){
            try {
                conn = getConn();
                pstat = conn.prepareStatement(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return pstat;
        }

        public static int update (String sql, Object...obj){
            pstat = getPstat(sql);
            int n = 0;
            try {
                for (int i = 0; i < obj.length; i++) {
                    pstat.setObject(i + 1, obj[i]);

                }
                n = pstat.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return n;
        }

        static ResultSet rs = null;
        public static ResultSet select (String sql, Object...obj){
            pstat = getPstat(sql);
            try {
                for (int i = 0; i < obj.length; i++) {
                    pstat.setObject(i + 1, obj[i]);
                }
                rs = pstat.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }

        public static void close () {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
